

 file open fh using .\Results\Table_Discrim_educ.tex, write replace 

 file write fh "{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{7}{c}}" _n ///
	"\toprule \toprule" _n ///
	"& (1) & (2) & (3) & (4) & (5) & (6)  \\" _n ///
	" Equal opport. to & \multicolumn{3}{c}{Men} & \multicolumn{3}{c}{White} \\ "_n ///
	" & \multicolumn{3}{c}{discriminated} & \multicolumn{3}{c}{discriminated}  \\ "_n ///
	"\cline{2-7}" _n
	
	local model discrim_educ
	local col = 6

* White*Male
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local var WhiteMale
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "White Male & `b'`star'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star'" 
}
if `i'==`col' {	
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star' \\" _n
	}
	}

* Second write p-values
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh " & (`pval')" 
}
if `i' >1 & `i'<`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval')" 
}
if `i' ==`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval') " _n
}
	}	
	
	
	
	* The commands below complete the document
file write fh  "\\ \midrule" _n  ///
				"Sample &          No qualif./answer       &   High School & University &          No qualif./answer       &   High School & University \\[0.4ex]" _n 
* Adding mean
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "Mean dep variable      & `mean'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean'" 
}
if `i'==`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean' \\[0.4ex] " _n
}
	}
				
file write fh	"Individual controls & $\checkmark$ & $\checkmark$ & $\checkmark$ & $\checkmark$ & $\checkmark$ & $\checkmark$  \\[0.4ex]" _n
* Adding observations
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "N.obs      & `N'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N'" 
}
if `i'==`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N' \\ \bottomrule \bottomrule " _n
}
	}
				
file write fh		"\end{tabular}}" _n 
file close fh

